<?php
include_once '../include/lxbase.class.php';
include_once '../include/wx.class.php';
include_once '../public/func.php';
$wxpay = new Wx();
$wxpay->setinit("api");
$wxpaydata = $_GET["wxpaydata"];
$data = json_decode(base64_decode($wxpaydata),true);
if(!empty($data["logid"])){
	$temp = $wxpay->viewdata("info","hotel_pay_log","where id={$data['logid']}");
	$info = json_decode(base64_decode($temp["info"]),true);
	foreach($info as $k=>$v){
		$data[$k]=$info[$k];
	}
	$wxpay->deldata("hotel_pay_log","where id={$data['logid']}");
}

//企悦用户UID
if(!empty($data["userid"])){
	$userid=$data["userid"];
}elseif(!empty($data["token"])){
	$userid=$wxpay->oncedata("id","users","where token='".$data["token"]."'");
}
if(empty($userid)){die("用户标识不能为空");}
$wxinfo = $wxpay->viewdata("*","wxuser","where userid={$userid}");
if(empty($wxinfo)){die("未获取到用户微信配置信息");}
//订单编号
$sn=$data["sn"];
if(empty($sn)){die("订单编号不能为空");}
//订单标题
$title=$data["title"];
if(empty($title)){die("订单标题不能为空");}
//订单内容
$body=$data["body"];
if(empty($body)){die("订单内容不能为空");}
//订单价格
$price=$data["price"];
if(empty($price)){die("订单价格错误");}
//支付结束跳转链接
$backurl=$data["url"];
if(empty($backurl)){die("回跳地址不能为空");}
//异步通知地址
$nurl = empty($data["nurl"])?$backurl:$data["nurl"];
//客户端IP
$ip=$data["ip"];
if(empty($ip)){die("客户IP不能为空");}
if(empty($wxinfo["shopid"])){die("用户商户号未设置");}
if(empty($wxinfo["shopkey"])){die("用户商户秘钥未设置");}
if(!empty($_COOKIE["openid"])){
	$openid=$_COOKIE["openid"];
}elseif(!empty($data["openid"])){
	$openid=$data["openid"];
	setcookie("openid",$openid,time()+7201,"/");
}else{
	$newid = $wxpay->setdata("hotel_pay_log","orderid=0,tim=now(),openid='',info='{$wxpaydata}'");
	$callbackurl=urlencode($wxpay->basehost."api/getopenid.php");
	$pay["appid"] = $wxinfo["appid"];
	$pay["secret"] = $wxinfo["secret"];
	$pay["logid"] = $newid;
	$state=base64_encode(json_encode($pay));
	header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid={$wxinfo['appid']}&redirect_uri={$callbackurl}&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect");
	exit;
}

$noncestr=getrandstr(16);
//配置微信支付信息
$wxdata["appid"]              = $wxinfo["appid"];
$wxdata["attach"]             = $title;
$wxdata["body"]               = $body;
$wxdata["mch_id"]             = $wxinfo["shopid"];
$wxdata["nonce_str"]          = $noncestr;
$wxdata["notify_url"]         = urlencode($nurl);
$wxdata["openid"]             = $openid;
$wxdata["out_trade_no"]       = $sn;
$wxdata["spbill_create_ip"]   = $ip;
$wxdata["total_fee"]          = $price*100;
$wxdata["trade_type"]='JSAPI';
//验证字符串未APPID+账号+商户ID
$wxdata = $wxpay->wxpaysign($wxdata,$wxinfo["shopkey"]);
$jsondata=$wxpay->weixin_pay($wxdata);
$url='https://api.mch.weixin.qq.com/pay/unifiedorder';
$json = $wxpay->call_post($url,$jsondata);
$tmpobj = simplexml_load_string($json,'SimpleXMLElement',LIBXML_NOCDATA);
$prepay_id = $tmpobj->prepay_id;
//前端签名验证
$jsdata["appId"]=$wxinfo["appid"];
$jsdata["timeStamp"]=time();
$jsdata["nonceStr"]=$noncestr;
$jsdata["package"]="prepay_id={$prepay_id}";
$jsdata["signType"] = 'MD5';
$jsdata = $wxpay->wxpayjsapi($jsdata,$wxinfo["shopkey"]);
//部署到前端展示
$wxpay->smarty->assign("jsdata",$jsdata);
$wxpay->smarty->assign("url",$backurl);
$wxpay->smarty->assign("price",$price);
$wxpay->smarty->assign("shopname",$wxinfo["wxtitle"]);
$wxpay->smarty->assign("title",$title);
$wxpay->smarty->display("wxpay.tpl");